package com.quarkus.pgsql.repositories;

import com.quarkus.pgsql.entities.AppUserEntity;
import io.quarkus.hibernate.orm.panache.PanacheQuery;
import io.quarkus.hibernate.orm.panache.PanacheRepository;
import io.quarkus.panache.common.Page;
import io.quarkus.panache.common.Parameters;
import jakarta.enterprise.context.ApplicationScoped;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@ApplicationScoped
public class AppUserRepository implements PanacheRepository<AppUserEntity> {

    public AppUserEntity findByLoginName(String loginName) {
        // 这里的query对应着AppUserEntity的字段名
        return find("loginName", loginName).firstResult();
    }

    public List<AppUserEntity> findByState(String state) {
        return find("state", state).list();
    }

    public List<AppUserEntity> findByUserGroupAndState(String userGroup, String state) {
        return find("userGroup = ?1 and state = ?2", userGroup, state).list();
    }

    public List<AppUserEntity> findByUserGroupAndState2(String userGroup, String state) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userGroup", userGroup);
        paramMap.put("state", state);

        Map<String, Object> map = Parameters.with("userGroup", userGroup).and("state", state).map();
        Parameters parameters = new Parameters();
        parameters.and("userGroup", userGroup).and("state", state);
        return find("userGroup = :userGroup and state = :state", paramMap).list();
    }

    public List<AppUserEntity> page(int pageSize, int pageIdx) {
        return findAll().page(pageIdx, pageSize).list();
    }


    public List<AppUserEntity> page2(int pageSize, int pageIdx) {
        Page page = Page.of(pageIdx, pageSize);
        return findAll().page(page).list();
    }

    public List<AppUserEntity> page3(int rangeStart, int rangeEnd) {
        return findAll().range(rangeStart,rangeEnd).list();
    }
}
